public class MaxSubarraySum {

     public static int maxSubarraySum(int[] nums) {
                if (nums == null || nums.length == 0) {
            throw new IllegalArgumentException("输入数组不能为空");
        }

        
        int currentMax = nums[0];
        int globalMax = nums[0];

              for (int i = 1; i < nums.length; i++) {
                        currentMax = Math.max(nums[i], currentMax + nums[i]);
                     globalMax = Math.max(globalMax, currentMax);
        }

        return globalMax;
    }

    public static void main(String[] args) {
        
        int[] arr1 = {1, -2, 3, 5, -1};
        System.out.println(maxSubarraySum(arr1)); 
                int[] arr2 = {1, -2, 3, -8, 5, 1};
        System.out.println(maxSubarraySum(arr2)); 

                int[] arr3 = {1, -2, 3, -2, 5, 1};
        System.out.println(maxSubarraySum(arr3)); 

        
        int[] arr4 = {-5, -3, -2};
        System.out.println(maxSubarraySum(arr4));     }
}